package com.pacific.sign.biz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pacific.common.exception.BizException;
import com.pacific.sign.dal.dao.pac.custom.RoleCustom;
import com.pacific.sign.model.po.pac.mbg.PacHcRole;
import com.pacific.sign.model.po.pac.request.PacHcRoleRequest;
import com.pacific.sign.model.po.pac.response.PacHcRoleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色
 */
@Service
public class RoleService {
    @Autowired
    private RoleCustom roleCustom;

    /**
     * 查询
     */
    public PacHcRoleResponse query(PacHcRoleRequest req){
        PacHcRoleResponse resp = new PacHcRoleResponse();
         /*	1.	分页请求	*/
        PageHelper.startPage(
                req.getPageNum() == null ? 1 : req.getPageNum(),
                req.getPageSize() == null ? 20 : req.getPageSize()
        );
        PacHcRole role = new PacHcRole();
        role.setType(req.getType());
        role.setCode(req.getCode());
        List<PacHcRole> list = roleCustom.selectByConditions(role);
          /* 2. 分页返回 */
        PageInfo pageInfo = new PageInfo(list);
        resp.setPageNum(pageInfo.getPageNum());
        resp.setPageSize(pageInfo.getPageSize());
        resp.setPages(pageInfo.getPages());
        resp.setTotal(pageInfo.getTotal());


        resp.setList(list);
        return resp;
    }

    public int insert(PacHcRole obj){
        int i = roleCustom.insert(obj);
        if(i <1){
            throw new BizException("新增失败");
        }
        return i;
    }

    public int update(PacHcRole obj){
        int i = roleCustom.updateById(obj);
        if(i <1){
            throw new BizException("更新失败");
        }
        return i;
    }

    public int delete(PacHcRole obj){
        if(obj == null || obj.getId() == null){
            throw new BizException("主键为空");
        }
        int i = roleCustom.deleteById(String.valueOf(obj.getId()));
        if(i <1){
            throw new BizException("删除失败");
        }
        return i;
    }

    public PacHcRole selectById(Integer id){
        if(id == null){
            throw new BizException("主键为空");
        }
        return roleCustom.selectById(String.valueOf(id));
    }


    /**
     * 根据级别码查询
     */
    public PacHcRole queryByCode(String code){
        return roleCustom.queryByCode(code);
    }
}
